﻿using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SignalR.Client
{
   public static class SignalRClientRegister
    {
     
        public static async Task<HubConnection> GetHubConnectionAsync(string CallByHubName)
        {
            string SignalR_Port = Environment.GetEnvironmentVariable("SignalR_Port", EnvironmentVariableTarget.Machine);//ipconfig为电脑环境变量名
            HubConnection connection = new HubConnectionBuilder().
              WithUrl("http://localhost:5198/chat").Build();
            //断线重连
            connection.Closed += async (Exception? error) =>
            {
                await Task.Delay(new Random().Next(0, 5) * 1000);
                await connection.StartAsync();
            };
            //注册服务端响应  被服务端调用
            connection.On<string>(CallByHubName, (string str) =>
            {

                Console.WriteLine(str);


            });
            //连接
            await connection.StartAsync();
            return connection;
        }

    }
}
